/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * interface.c : Main interface related routines.
 * Copyright (C) 2008, 2009 Lingtao Pan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */


#include "common.h"

#define MENU_UI PKGDATADIR "/menu.ui"

GtkActionEntry action_entries [] =
{
    { "File", NULL,  N_("_File"), NULL, NULL, NULL },
    { "Bookmarks", NULL, N_("_Bookmarks"), NULL, NULL, NULL },
    { "Transfer", NULL, N_("_Transfer"), NULL, NULL, NULL },
    { "Log", NULL, N_("L_og"), NULL, NULL, NULL },
    { "Tools", NULL, N_("Tool_s"), NULL, NULL, NULL },
    { "Help", NULL, N_("_Help"), NULL, NULL, NULL },
    { "Download", NULL, N_("_Download"), NULL, NULL, 
      G_CALLBACK (download_cb) },
    { "Upload", NULL, N_("_Upload"), NULL, NULL, 
      G_CALLBACK (upload_cb) },
    { "RefreshLeft", GTK_STOCK_REFRESH, N_("_Refresh"), NULL, NULL, 
      G_CALLBACK (refresh_left_cb) },
    { "RefreshRight", GTK_STOCK_REFRESH, N_("_Refresh"), NULL, NULL, 
      G_CALLBACK (refresh_right_cb) },
    { "CopyFilenameLeft", GTK_STOCK_COPY, N_("_Copy Name"), NULL, NULL, 
      G_CALLBACK (copy_filename_left_cb) },
    { "CopyFilenameRight", GTK_STOCK_COPY, N_("_Copy Name"), NULL, NULL, 
      G_CALLBACK (copy_filename_right_cb) },
    { "SetPreferences", GTK_STOCK_PREFERENCES, N_("_Preferences"), NULL, NULL,
      G_CALLBACK (show_preference_dialog_cb) },
    { "ShowBMEditor", NULL, N_("_Show BookMark Editor"), NULL, NULL, 
      G_CALLBACK (show_bookmark_editor) },
    { "HelpManual", GTK_STOCK_HELP, N_("_Contents"), "F1",
      N_("Help on this application"), 
      G_CALLBACK (show_help_cb) },
    { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL, 
      N_("About this application"),
      G_CALLBACK (show_about_cb) },
    
    { "DeleteFileLeft", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL,
      G_CALLBACK (delete_file_left_cb) },

    { "DeleteFileRight", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL,
      G_CALLBACK (delete_file_right_cb) },
};

GtkActionEntry transfer_action_entries [] =  {
    { "TransferStop", GTK_STOCK_MEDIA_PAUSE, N_("_Stop"), NULL, NULL, 
      G_CALLBACK (transfer_stop_cb) },
    { "TransferResume", GTK_STOCK_MEDIA_PLAY, N_("_Resume"), NULL, NULL, 
      G_CALLBACK (transfer_resume_cb) },
    { "TransferItemMoveUp", GTK_STOCK_GO_UP, N_("Move _Up"), NULL, NULL, 
      G_CALLBACK (transfer_item_move_up_cb) },
    { "TransferItemMoveDown", GTK_STOCK_GO_DOWN, N_("Move _Down"), NULL, NULL,
      G_CALLBACK (transfer_item_move_down_cb) },
    { "TransferCleanAll", GTK_STOCK_CLEAR, N_("_Clear"), NULL, NULL,
      G_CALLBACK (transfer_clean_all_cb) },
    { "TransferItemDelete", GTK_STOCK_REMOVE, N_("_Delete"), NULL, NULL,
      G_CALLBACK (transfer_item_delete_cb) },
};


/* GtkRadioActionEntry window_radio_entries [] = */
/* { */
/*   { "SwitchToWindow1", NULL, N_("Window _1"), "<control>1", */
/*     N_("Switch to window 1"), 1 }, */
/*   { "SwitchToWindow2", NULL, N_("Window _2"), "<control>2", */
/*     N_("Switch to window 2"), 2 } */
/* }; */

static void gftp_interface_setup (GftpInterface *gftp_interface);
static void create_main_window (GftpInterface *gftp_interface, GtkWidget *window);
static GtkWidget *create_connect_toolbar ();
static gint gftp_try_close (GtkWidget * widget, GdkEvent * event, gpointer data);
static void gftp_force_close (GtkWidget * widget, gpointer data);
static void set_icon (GtkWindow *window);
static void setup_log_window (GftpInterface *gftp_interface);
static void set_callbacks (GftpInterface *gftp_interface);
static void create_stocks ();
static void add_bookmarks ();


GftpInterface *
gftp_interface_new ()
{
    GftpInterface *intf;
    intf = (GftpInterface *)g_malloc0 (sizeof (GftpInterface));

    gftp_interface_setup (intf);
    return intf;
}


static void
gftp_interface_setup (GftpInterface *gftp_interface)
{
    GtkWidget *window;

    gftp_interface->window = window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (GTK_WIDGET(window), 1024, 600);

    gtk_signal_connect (GTK_OBJECT(window), "delete_event",
                        GTK_SIGNAL_FUNC(gftp_try_close), NULL);
    gtk_signal_connect (GTK_OBJECT(window), "destroy",
                        GTK_SIGNAL_FUNC(gftp_force_close), NULL);
    gtk_window_set_title (GTK_WINDOW(window), gftp_version);
    gtk_window_set_wmclass (GTK_WINDOW (window), "main", "gftp2");
    gtk_widget_set_name (window, gftp_version);
    gtk_widget_realize (window);
    
    set_icon (GTK_WINDOW (window));
    create_stocks ();

    create_main_window (gftp_interface, window);

    add_bookmarks ();

    gtk_widget_show_all (window); 
    gtk_widget_hide (GTK_WIDGET(gftp_interface->prgbar));
}

static void
create_main_window (GftpInterface *gftp_interface, GtkWidget *window)
{
    GtkWidget *winpane, *dlpane, *mainvbox, *tempwid, *notebook;
    GtkWidget *menubar, *toolbar;
    GtkWidget *hbox;
    GtkWidget *vbox, *btn;
    GtkWidget *image, *label;
    GtkShadowType shadow_type;
    GError *error = NULL;
  

    mainvbox = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), mainvbox);
    
    /* Build Menus */
    ui_manager = gtk_ui_manager_new ();

    gftp_interface->main_action_group = gtk_action_group_new ("MainActionGroup");
    gtk_action_group_set_translation_domain (gftp_interface->main_action_group,
                                             PACKAGE);

    gtk_action_group_add_actions (gftp_interface->main_action_group, 
                                  action_entries,
                                  G_N_ELEMENTS(action_entries), NULL);  
    gtk_ui_manager_insert_action_group (ui_manager, 
                                  gftp_interface->main_action_group, 0);
  
    gftp_interface->transfer_action_group = 
        gtk_action_group_new ("TransferActionGroup");
    gtk_action_group_add_actions (gftp_interface->transfer_action_group, 
                                  transfer_action_entries,
                                  G_N_ELEMENTS(transfer_action_entries), NULL);  
    gtk_ui_manager_insert_action_group (ui_manager, 
                                  gftp_interface->transfer_action_group, 0);
    

    gtk_ui_manager_add_ui_from_file (ui_manager, MENU_UI, &error);
    if (error != NULL) {
        g_error ("Adding ui file fails: %s", error->message);
    }
  

    menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
    g_assert (menubar != NULL);
    gtk_box_pack_start (GTK_BOX (mainvbox), menubar, FALSE, FALSE, 0);


    /* The Toolbar */
    toolbar = create_connect_toolbar (gftp_interface);
    gtk_box_pack_start(GTK_BOX (mainvbox), toolbar, FALSE, FALSE, 1);
  
    /* set up all panes first */
    winpane = gtk_hpaned_new ();
    gtk_container_set_border_width (GTK_CONTAINER (winpane), 4);

    dlpane = gtk_vpaned_new ();
    gtk_paned_pack1 (GTK_PANED (dlpane), winpane, TRUE, TRUE);

    notebook = gtk_notebook_new ();
    gtk_paned_pack2 (GTK_PANED (dlpane), notebook, TRUE, TRUE);

    gtk_box_pack_start (GTK_BOX (mainvbox), dlpane, TRUE, TRUE, 0);


    /* log window */
    gftp_interface->logwdw = gtk_text_view_new ();
    gtk_text_view_set_editable (GTK_TEXT_VIEW (gftp_interface->logwdw), FALSE);
    gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (gftp_interface->logwdw), 
                                      FALSE);
    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (gftp_interface->logwdw), 
                                 GTK_WRAP_WORD);
    setup_log_window (gftp_interface);
  
    tempwid = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tempwid),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);
    gtk_container_add (GTK_CONTAINER (tempwid), gftp_interface->logwdw);

    label = gtk_label_new (_("Log"));    
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), tempwid, label);
  

    /* download window */
    transfer_view_setup (gftp_interface); 
    gtk_widget_set_size_request (gftp_interface->dlbox, -1, 150);

    label = gtk_label_new (_("Transfer"));    
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), gftp_interface->dlbox, 
                              label);
  


    /* create viewers */  
    hbox = gtk_hbox_new (FALSE, 0);
    gtk_paned_pack1 (GTK_PANED (winpane), hbox, TRUE, TRUE);

    /* create left viewer */
    left_viewer = gftp_viewer_new ();
    gtk_widget_set_size_request (GTK_WIDGET(left_viewer->frame), 500, 350);
    gtk_box_pack_start (GTK_BOX (hbox), left_viewer->frame, TRUE, TRUE, 1);

    /* create vbutton box */
    vbox = gtk_vbox_new (TRUE, 0);

    image = gtk_image_new_from_stock(GTK_STOCK_GO_BACK, GTK_ICON_SIZE_BUTTON);
    gftp_interface->dldbtn = gtk_button_new ();
    gtk_button_set_image (GTK_BUTTON (gftp_interface->dldbtn), image);
    gtk_box_pack_start (GTK_BOX (vbox), gftp_interface->dldbtn, FALSE, FALSE, 0);

    image = gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON);
    gftp_interface->uldbtn = btn = gtk_button_new ();
    gtk_button_set_image (GTK_BUTTON (gftp_interface->uldbtn), image);
    gtk_box_pack_start (GTK_BOX (vbox), gftp_interface->uldbtn, FALSE, FALSE, 0);
  
    gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 1);
  
  

    /* create right viewer */
    right_viewer = gftp_viewer_new ();
    gtk_widget_set_size_request (GTK_WIDGET(right_viewer->frame), 500, 350);
    gtk_paned_pack2 (GTK_PANED (winpane), right_viewer->frame, TRUE, TRUE);  
  

    /* -------- status bar -------- */
    hbox = gtk_hbox_new (TRUE, 0);
    gtk_box_pack_start (GTK_BOX (mainvbox), hbox, FALSE, FALSE, 0);

    
    gftp_interface->statusbar = (GtkStatusbar *)gtk_statusbar_new ();
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (gftp_interface->statusbar), 
                        TRUE, TRUE, 0);  
    gtk_statusbar_set_has_resize_grip (gftp_interface->statusbar, FALSE);


    gftp_interface->prgbar = (GtkProgressBar*) gtk_progress_bar_new ();
    gtk_widget_style_get (GTK_WIDGET (gftp_interface->statusbar), "shadow-type",
                          &shadow_type, NULL);
    tempwid = gtk_frame_new (NULL);
    gtk_frame_set_shadow_type (GTK_FRAME (tempwid), shadow_type);
    gtk_box_pack_end (GTK_BOX (gftp_interface->statusbar),
                        tempwid, FALSE, FALSE, 0);
    vbox = gtk_vbox_new (FALSE, 0);    
	gtk_container_add (GTK_CONTAINER(tempwid), vbox);
	gtk_widget_set_size_request (GTK_WIDGET (gftp_interface->prgbar), -1, 12);
    gtk_box_pack_end (GTK_BOX(vbox), GTK_WIDGET(gftp_interface->prgbar),
                        FALSE, TRUE, 0);

    gftp_interface->dlstatus = (GtkStatusbar *)gtk_statusbar_new ();
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (gftp_interface->dlstatus), 
                        TRUE, TRUE, 0);  
  

    /* set callbacks */
    set_callbacks (gftp_interface);
}

static GtkWidget *
create_connect_toolbar (GftpInterface *gftp_interface)
{
    GtkWidget *toolbar, *box, *tempwid;
    GtkWidget *image;
    int i, j, num = 0;
    char *default_protocol;

    toolbar = gtk_handle_box_new ();
    gtk_widget_set_size_request (toolbar, -1, 40);

    box = gtk_hbox_new (FALSE, 4);
    gtk_container_add (GTK_CONTAINER (toolbar), box);
    gtk_container_border_width (GTK_CONTAINER (box), 5);

    image = gtk_image_new_from_file (PKGDATADIR "/connect.xpm");
    gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);

  /* gftp_interface->openurl_btn = gtk_button_new (); */
  /* gtk_button_set_image (GTK_BUTTON (gftp_interface->openurl_btn), image); */

  /* gtk_container_border_width (GTK_CONTAINER (gftp_interface->openurl_btn), 0); */
  /* gtk_box_pack_start (GTK_BOX (box), gftp_interface->openurl_btn, FALSE, FALSE, 0); */

  /* gtk_button_set_relief (gftp_interface->openurl_btn, GTK_RELIEF_NONE); */

  
    tempwid = gtk_label_new_with_mnemonic (_("_Host: "));
    gtk_box_pack_start (GTK_BOX (box), tempwid, FALSE, FALSE, 0);
    gftp_interface->hostedit = gtk_combo_box_entry_new_text ();
    gtk_widget_set_size_request (gftp_interface->hostedit, 160, 15);
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->hostedit, 
                        TRUE, TRUE, 0);

    tempwid = gtk_label_new (_("Port:"));
    gtk_box_pack_start (GTK_BOX (box), tempwid, FALSE, FALSE, 0);
    gftp_interface->portedit = gtk_combo_box_entry_new_text ();
    gtk_widget_set_size_request (gftp_interface->portedit, 80, -1);
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->portedit, 
                        FALSE, FALSE, 0);
  
    tempwid = gtk_label_new_with_mnemonic (_("_User:"));
    gtk_box_pack_start (GTK_BOX (box), tempwid, FALSE, FALSE, 0);
    gftp_interface->useredit = gtk_combo_box_entry_new_text ();
    gtk_widget_set_size_request (gftp_interface->useredit, 120, -1);
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->useredit, 
                        FALSE, FALSE, 0);
  
    tempwid = gtk_label_new_with_mnemonic (_("_Pass:"));
    gtk_box_pack_start (GTK_BOX (box), tempwid, FALSE, FALSE, 0);
    gftp_interface->passedit = gtk_entry_new ();
    gtk_widget_set_size_request (gftp_interface->passedit, 100, 32);
    gtk_entry_set_visibility (GTK_ENTRY (gftp_interface->passedit), FALSE);
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->passedit, 
                        FALSE, FALSE, 0);
  
    /* protocol choosen */
    gftp_interface->optionmenu = gtk_combo_box_new_text ();
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->optionmenu, 
                        FALSE, FALSE, 0);

    default_protocol = gftp_config_get_string ("General", "default_protocol");
    if (default_protocol == NULL)
        default_protocol = g_strdup ("FTP");
    
    for (i=0, j=0; gftp_protocols[i].name; i++) {
        if (!gftp_protocols[i].shown)
            continue;

        gtk_combo_box_append_text (GTK_COMBO_BOX (gftp_interface->optionmenu),
             gftp_protocols[i].name);

        if (default_protocol != NULL &&
            strcmp (gftp_protocols[i].name, default_protocol) == 0)
        num = j;
    
        j++;
    }
    g_free (default_protocol);

    /* this function must be called after all texts are inserted */
    gtk_combo_box_set_active (GTK_COMBO_BOX (gftp_interface->optionmenu),
                              num);

    tempwid = gtk_image_new_from_stock (GTK_STOCK_STOP,
                                        GTK_ICON_SIZE_MENU);

    gftp_interface->stop_btn = gtk_button_new ();
    gtk_container_add (GTK_CONTAINER (gftp_interface->stop_btn), tempwid);
    gtk_widget_set_sensitive (gftp_interface->stop_btn, 0);
    /* gtk_container_border_width (GTK_CONTAINER (gftp_interface->stop_btn), 0); */
    gtk_box_pack_start (GTK_BOX (box), gftp_interface->stop_btn,
                        FALSE, FALSE, 0);
  
    return toolbar;  
}

static gint
gftp_try_close (GtkWidget * widget, GdkEvent * event, gpointer data)
{
  /*
  if (gftp_file_transfers == NULL)
    {
      _gftp_exit (NULL, NULL);
      return (0);
    }
  else
    {
      MakeYesNoDialog (_("Exit"), _("There are file transfers in progress.\nAre you sure you want to exit?"), _gftp_exit, NULL, NULL, NULL);
      return (1);
    }
  */
    gtk_main_quit ();

    return 0;
}

static void
gftp_force_close (GtkWidget * widget, gpointer data)
{
    exit (0);
}

static void
set_icon (GtkWindow *window)
{
    GList *list = NULL;
    GdkPixbuf *icon;

    icon = gdk_pixbuf_new_from_file (PKGDATADIR "/gftp-16x16.png", NULL);
    if (icon) {
        list = g_list_append (NULL, icon);
    } else {
        g_warning ("can't load icon %s.",  PKGDATADIR "/gftp-16x16.png");
    }

    icon = gdk_pixbuf_new_from_file (PKGDATADIR "/gftp-22x22.png", NULL);
    if (icon) {
        list = g_list_append (list, icon);
    } else {
        g_warning ("can't load icon %s.",  PKGDATADIR "/gftp-22x22.png");
    }

    icon = gdk_pixbuf_new_from_file (PKGDATADIR "/gftp-24x24.png", NULL);
    if (icon) {
        list = g_list_append (list, icon);
    } else {
        g_warning ("can't load icon %s.",  PKGDATADIR "/gftp-24x24.png");
    }

    icon = gdk_pixbuf_new_from_file (PKGDATADIR "/gftp-32x32.png", NULL);
    if (icon) {
        list = g_list_append (list, icon);
    } else {
        g_warning ("can't load icon %s.",  PKGDATADIR "/gftp-32x32.png");
    }

    icon = gdk_pixbuf_new_from_file (PKGDATADIR "/gftp-48x48.png", NULL);
    if (icon) {
        list = g_list_append (list, icon);
    } else {
        g_warning ("can't load icon %s.",  PKGDATADIR "/gftp-48x48.png");
    }

    gtk_window_set_icon_list (window, list);
}

static void
set_callbacks (GftpInterface *gftp_interface)
{
  /* for the toolbar */
  /* g_signal_connect (gftp_interface->openurl_btn, "clicked", */
  /*                   G_CALLBACK (cb_openurl), NULL); */
    g_signal_connect (gftp_interface->stop_btn, "clicked",
                      G_CALLBACK (stop_cb), NULL);
    
    g_signal_connect (gftp_interface->dldbtn, "clicked",
                      G_CALLBACK (download_cb), NULL);

    g_signal_connect (gftp_interface->uldbtn, "clicked",
                      G_CALLBACK (upload_cb), NULL);
  
  
    g_signal_connect (gtk_bin_get_child(GTK_BIN (gftp_interface->hostedit)), 
                      "activate", G_CALLBACK (openurl_edit_finish_cb), NULL);
  
    g_signal_connect (gtk_bin_get_child(GTK_BIN (gftp_interface->useredit)), 
                      "activate", G_CALLBACK (openurl_edit_finish_cb), NULL);

    g_signal_connect (gftp_interface->passedit, 
                      "activate", G_CALLBACK (openurl_edit_finish_cb), NULL);

    g_signal_connect (gtk_bin_get_child(GTK_BIN (gftp_interface->portedit)), 
                      "activate", G_CALLBACK (openurl_edit_finish_cb), NULL);

    g_signal_connect (right_viewer->treeview, "button_press_event",
                      G_CALLBACK (right_view_button_click_cb), NULL);

    g_signal_connect (left_viewer->treeview, "button_press_event",
                      G_CALLBACK (left_view_button_click_cb), NULL);

    g_signal_connect (left_viewer->treeview, "enter-notify-event",
                      G_CALLBACK (viewer_enter_leave_cb), left_viewer);

    g_signal_connect (left_viewer->treeview, "leave-notify-event",
                      G_CALLBACK (viewer_enter_leave_cb), left_viewer);

    /* g_signal_connect (right_viewer->treeview, "enter-notify-event", */
    /*                   G_CALLBACK (view_enter_leave_cb), right_viewer);*/

    /* g_signal_connect (right_viewer->treeview, "leave-notify-event", */
    /*                   G_CALLBACK (view_enter_leave_cb), right_viewer);*/
}

static void setup_log_window (GftpInterface *gftp_interface)
{
    GtkTextView *view;
    GtkTextBuffer *textbuf;
    GtkTextIter iter;
    GtkTextTag *tag;
    GdkColor fore;

    view = GTK_TEXT_VIEW (gftp_interface->logwdw);
    textbuf = gtk_text_view_get_buffer (view);
  
    tag = gtk_text_buffer_create_tag (textbuf, "send", NULL);
    /* gftp_lookup_global_option ("send_color", &fore); */
    gdk_color_parse ("#008600", &fore);
    g_object_set (G_OBJECT (tag), "foreground_gdk", &fore, NULL);

    tag = gtk_text_buffer_create_tag (textbuf, "recv", NULL);
    /* gftp_lookup_global_option ("recv_color", &fore); */
    gdk_color_parse ("#0000ff", &fore);
    g_object_set (G_OBJECT (tag), "foreground_gdk", &fore, NULL);

    tag = gtk_text_buffer_create_tag (textbuf, "error", NULL);
    /* gftp_lookup_global_option ("error_color", &fore); */
    gdk_color_parse ("#ff0000", &fore);
    g_object_set (G_OBJECT (tag), "foreground_gdk", &fore, NULL);

    tag = gtk_text_buffer_create_tag (textbuf, "misc", NULL);
    /* gftp_lookup_global_option ("misc_color", &fore); */
    gdk_color_parse ("#808000", &fore);
    g_object_set (G_OBJECT (tag), "foreground_gdk", &fore, NULL); 

    gtk_text_buffer_get_start_iter (textbuf, &iter);
    gftp_interface->logwdw_textmark = gtk_text_buffer_create_mark (textbuf, 
                                          "end", &iter, FALSE); 
}



struct gtkui_file_pdata*
alloc_file_pdata (gftp_file *fle)
{
    struct gtkui_file_pdata *uidata;

    uidata = g_malloc0 (sizeof(struct gtkui_file_pdata));
    fle->user_data = uidata;
    
    return uidata;
}

static GList *bookmark_widget_list;

static void add_bookmark_widget_list (GtkWidget *menu_item)
{
    bookmark_widget_list = g_list_prepend (bookmark_widget_list, menu_item);
}

static void
_add_bookmarks_r (GNode *entry, GtkMenu *menu)
{
    GNode *childNode;
    gftp2_bookmark *child;
    GtkWidget *menu_item;
    GtkWidget *child_menu;
    gchar *bm_name;
    char *pos;

    childNode = entry->children;

    while (childNode != NULL) {
        child = (gftp2_bookmark *) childNode->data;
        bm_name = child->name;

        menu_item = gtk_menu_item_new_with_label (bm_name);
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
        add_bookmark_widget_list (menu_item);
    
        if (childNode->children != NULL) {
            child_menu = gtk_menu_new ();
            gtk_menu_item_set_submenu (GTK_MENU_ITEM(menu_item), child_menu);
            add_bookmark_widget_list (child_menu);
            _add_bookmarks_r (childNode, GTK_MENU(child_menu));
        } else {
            g_signal_connect (G_OBJECT (menu_item), "activate",
                              G_CALLBACK (bookmark_activated_cb), child);
        }

        childNode = childNode->next;
    }
}

void
add_bookmarks ()
{
    GtkWidget *bmenu = NULL, *menu;
    GtkWidget *menu_item;

    bmenu = gtk_ui_manager_get_widget (ui_manager, "/MenuBar/BookmarksMenu");
    menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (bmenu));

    menu_item = gtk_separator_menu_item_new ();
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
    add_bookmark_widget_list (menu_item);

    if (bookmark_root && bookmark_root->children != NULL) {
        _add_bookmarks_r (bookmark_root, GTK_MENU(menu));
    }

    gtk_widget_show_all (menu);
}

static void remove_bookmarks ()
{
    GList *ptr;
    GtkWidget *widget;

    for (ptr = bookmark_widget_list; ptr; ptr = ptr->next) {
        widget = (GtkWidget *)ptr->data;
        gtk_widget_destroy (widget);
    }
    g_list_free (bookmark_widget_list);
    bookmark_widget_list = NULL;
}

void refresh_bookmark_menu ()
{
    remove_bookmarks ();
    add_bookmarks ();
}


static guint update_prgbar_id;

gboolean  update_progress_bar (gpointer data)
{
    GftpInterface *interface = (GftpInterface *) data;

    gtk_progress_bar_pulse (interface->prgbar);

    return TRUE;
}

void enable_progressbar (GftpInterface *interface)
{
    gtk_widget_show (GTK_WIDGET (interface->prgbar));
    update_prgbar_id = g_timeout_add (100,
                       update_progress_bar, interface);
}

void disable_progressbar (GftpInterface *interface)
{
    gtk_widget_hide (GTK_WIDGET (interface->prgbar));
    g_source_remove (update_prgbar_id);
}

static void add_stock_icon (GtkIconFactory *factory,
                            gchar *location,
                            gchar *stock_id)
{
    GtkIconSource *source;
    GtkIconSet *set;
    
    source = gtk_icon_source_new ();
    set = gtk_icon_set_new ();
  
    gtk_icon_source_set_filename (source, location);
    gtk_icon_set_add_source (set, source);
    gtk_icon_factory_add (factory, stock_id, set);
}

typedef struct
{
    gchar *location;
    gchar *stock_id;
} NewStockIcon;

const NewStockIcon list[] =
{
    { PKGDATADIR "/sound.png", "sound" },
    { PKGDATADIR "/video.png", "video" },
    { PKGDATADIR "/package.png", "package" },
    { PKGDATADIR "/iso.png",   "iso" },
    { NULL, NULL }
};

static void create_stocks ()
{
    GtkIconFactory *factory;
    int i = 0;
    
    factory = gtk_icon_factory_new ();
    
    while (list[i].location != NULL) {
        add_stock_icon (factory, list[i].location, list[i].stock_id);
        i++;
    }

    gtk_icon_factory_add_default (factory);
}
